using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.Entidades;

namespace Marlimer.Data.Comercial
{
    public class his_AvancesVenta
    {
        public DataTable getAvancesVenta(int anio, int mes)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sqlcmd.CommandText = "spsConsultaAvancesVentas";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@NANIO", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NMES", SqlDbType.Int);

            sqlcmd.Parameters["@NANIO"].Value = anio;
            sqlcmd.Parameters["@NMES"].Value = mes;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0];
        }

        public resulMsgError updAvancesVenta(List<InfoAvancesVenta> aventas)
        {
            resulMsgError resul = new resulMsgError(false, "");
            //creamos el objeto de transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("saveobj");

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandText = "spuActualizaAvancesVentas";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
            sqlcmd.Transaction = transac; 

            sqlcmd.Parameters.Add("@NANIO", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NMES", SqlDbType.Int);
            sqlcmd.Parameters.Add("@CCOD_GRPVTA", SqlDbType.Int);
            sqlcmd.Parameters.Add("@CCOD_ZONA", SqlDbType.Int);
            sqlcmd.Parameters.Add("@CCOD_RUTA", SqlDbType.Int);
            sqlcmd.Parameters.Add("@CCOD_PROD", SqlDbType.Int);
            sqlcmd.Parameters.Add("@NCANT_OBJ", SqlDbType.Decimal);
            sqlcmd.Parameters.Add("@NSOLES_OBJ", SqlDbType.Decimal);

            try
            {

                for (int i = 0; i < aventas.Count; i++)
                {
                    sqlcmd.Parameters["@NANIO"].Value = aventas[i].nanio;
                    sqlcmd.Parameters["@NMES"].Value = aventas[i].nmes;
                    sqlcmd.Parameters["@CCOD_GRPVTA"].Value = aventas[i].ccod_grpvta;
                    sqlcmd.Parameters["@CCOD_ZONA"].Value = aventas[i].ccod_zona;
                    sqlcmd.Parameters["@CCOD_RUTA"].Value = aventas[i].ccod_ruta;
                    sqlcmd.Parameters["@CCOD_PROD"].Value = aventas[i].ccod_prod;
                    sqlcmd.Parameters["@NCANT_OBJ"].Value = aventas[i].ncant_obj;
                    sqlcmd.Parameters["@NSOLES_OBJ"].Value = aventas[i].nsoles_obj;
                    sqlcmd.ExecuteNonQuery();
                }
                transac.Commit(); 
                resul.resul = true;
            }
            catch (Exception ex)
            {
                transac.Rollback();
                resul.message = ex.Message;
            }
            return resul;
        }

    }
}
